/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.productgold;

import com.kmcsoft.core.ui.GenericController;
import com.kmcsoft.goldshop.model.master.ProductGold;
import com.kmcsoft.goldshop.productgold.service.ProductGoldService;
import com.kmcsoft.utils.i18n;
import com.kmcsoft.zkex.PagingListModel;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.impl.MessageboxDlg;

/**
 *
 * @author kmcsoft02
 */
public class ProductGoldComposer extends GenericController<ProductGold, ProductGoldService> {

    public void onCreate() {
        onClick$new(null);
    }
    
    //<editor-fold defaultstate="collapsed" desc="PagingListModel">
    @Override
    public PagingListModel getListModel() {
//        PagingListModel<ProductJewelry> model = Utilities.getPagingListModel(objectClass, HashMap.class, "id, status ,createdDate, barcode, productType");
        PagingListModel<ProductGold> model = super.getListModel();
        if (model != null) {

            model.setFilter("productType == pType");
            model.setParameters("java.lang.Integer pType");
            model.setParameterValues(Integer.valueOf(ProductGold.TYPE_GOLD));
        }
        model.setOrdering("createdDate descending");
        model.initialize();
        return model;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doAfterNew">
    @Override
    protected void doAfterNew() {
        super.doAfterNew();
        selected.setType(ProductGold.TYPE_18K);
        selected.setProductType(ProductGold.TYPE_GOLD);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="setSelected">
    @Override
    public void setSelected(ProductGold selected) {
        if (selected.getId() == 0) {
            super.setSelected(selected);
        } else {
            super.setSelected(service.getProductGoldById(selected.getId()));
        }
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doBeforeSave">
    @Override
    protected void doBeforeSave() {
        super.doBeforeSave();
        if (isExistName(selected)) {
            throw new WrongValueException(i18n.l("shop.goldType.error.duplicate.name"));
        }
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="isExistName">
    public boolean isExistName(ProductGold productGold) {
        ProductGold pg = service.getObject(ProductGold.class, "name.toLowerCase()==pName", "String pName", productGold.getName().toLowerCase());
        if (pg == null) {
            return false;
        }
        if (pg.getId() == productGold.getId()) {
            return false;
        }
        return true;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onClick$delete">
    @Override
    public void onClick$delete(ForwardEvent fe) throws InterruptedException {
        if (selected.isReadonly()) {
            throw new WrongValueException(i18n.l("shop.goldType.error.delete"));
        }
        Messagebox.show(i18n.l("shop.dialogBox.warming.question"), i18n.l("shop.dialogBox.title"),
                Messagebox.YES | Messagebox.NO, Messagebox.QUESTION, new EventListener() {
                    
                    public void onEvent(Event event) throws Exception {
                        if ("onYes".equalsIgnoreCase(event.getName())) {
                            ((MessageboxDlg) event.getTarget()).detach();
                            service.deletePersistent(selected);
                            doAfterDelete();
                            getBinder().loadComponent(leftList);
                            onClick$new(null);
                        }
                    }
                });
    }
    //</editor-fold>
}
